package com.fy.market.trigger.http;

import com.fy.market.persistent.mapper.UserMapper;
import com.fy.market.persistent.po.User;
import com.fy.market.strategy.model.entity.DecrAwardStockResultEntity;
import com.fy.market.strategy.model.entity.RuleMatterEntity;
import com.fy.market.strategy.repository.IStrategyRepository;
import com.fy.market.strategy.service.raffle.IRaffleStrategy;
import com.fy.market.trigger.api.StrategyRaffleApi;
import com.fy.market.types.common.reslut.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.sql.Timestamp;
import java.util.Date;

import static com.fy.market.types.common.reslut.R.ok;

/**
 * 抽奖接口
 * @author fwq
 * @date 2025-08-11 10:00
 * @desc 抽奖接口
 */
@Slf4j
@Tag(name = "策略抽奖接口", description = "这个是用来抽奖的接口")
@RestController
@RequestMapping("/raffle")
public class StrategyRaffleController implements StrategyRaffleApi {

    @Resource
    private IRaffleStrategy raffleStrategy;

    @Resource
    private IStrategyRepository iStrategyRepository;

    /**
     * 抽奖
     * @param userId 用户ID
     * @param strategyId 策略ID
     * @return 抽奖结果
     */
    @Override
    @Operation(summary = "抽奖接口", description = "根据用户ID和策略ID进行抽奖")
    @GetMapping("doRaffle")
    public R doRaffle(Integer userId, Integer strategyId) {
        log.info("控制层已接收，策略ID："+strategyId);
        RuleMatterEntity ruleMatterEntity = RuleMatterEntity.builder()
                .userId(userId)
                .strategyId(strategyId)
                .build();
        return raffleStrategy.doRaffle(ruleMatterEntity);
    }


    /**
     * 模拟扣减库存
     *
     * @param strategyId 策略ID
     * @param awardId    奖品ID
     * @return 扣减库存结果
     */
    @Override
    @Operation(summary = "模拟扣减库存")
    @GetMapping("/decrAwardStock")
    public R decrAwardStock(@RequestParam("strategyId") Integer strategyId, @RequestParam("awardId") Integer awardId) {

        DecrAwardStockResultEntity decrAwardStockResultEntity = iStrategyRepository.decrAwardStock(strategyId, awardId);
        if (!decrAwardStockResultEntity.getCODE().equals(0))
            log.error("扣减奖品库存失败，策略ID: {}, 奖品ID: {}, 错误码: {}", strategyId, awardId, decrAwardStockResultEntity.getCODE());
        return ok(decrAwardStockResultEntity);
    }



    @Resource
    private UserMapper userMapper;
    // 测试添加用户信息
    @Override
    @Operation(summary = "添加用户接口", description = "根据用户ID和策略ID进行抽奖")
    @GetMapping("addUser")
    public R addUser() {
        User user = new User();
        // 全部使用虚拟数据 account 作为分表键，随机生成插入20条测试数据
        // 随机生成用户ID
        for (int i = 0; i < 50; i++) {
            user.setAccount(String.valueOf(i));
            user.setPassword("password");
            user.setNickName("nickName");
            user.setAvatarUrl("avatarUrl");
            user.setStatus(1);
            user.setRole(0);
            user.setCreateTime(new Date());
            user.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            user.setDeleted(0);
            userMapper.add(user);
        }
        return ok();
    }

    /**
     * 查询用户信息
     * @param account 用户账号
     * @return 用户信息
     */
    @Override
    @Operation(summary = "查询用户接口", description = "根据用户账号查询用户信息")
    @GetMapping("getUser")
    public R<User> getUser(String account) {
        return ok(userMapper.get(account).get(0));
    }
}
